package com.itboye.mengwu.luntan.comment.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.config.GlobalConfig;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiBaoHanReply;
import com.itboye.mengwu.data.model.TieZiModel;
import com.itboye.mengwu.luntan.comment.bean.Comment;
import com.itboye.mengwu.luntan.comment.view.MyListView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MyTieZiCommentAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private ViewHolder viewHolder;
	private Context context;
	private OnClickListener replyToCommentListener;
	private OnClickListener replyToReplyListener;
	private OnClickListener dianzanListener;
	private ArrayList<TieZiBaoHanReply.GetAList> commentList;
	private OnClickListener guanzhuOnclickListener;

	public MyTieZiCommentAdapter(Context context,
			ArrayList<TieZiBaoHanReply.GetAList> commentList,
			OnClickListener replyToCommentListener,
			MyTieZiCommentReplyAdapter myAdapter,
			OnClickListener replyToReplyListener,
			OnClickListener dianzanListener,
			OnClickListener guanzhuOnclickListener) {
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		// this.commentList = new ArrayList<TieZiBaoHanReply.GetAList>();
		this.commentList = commentList;
		this.replyToCommentListener = replyToCommentListener;
		this.replyToReplyListener = replyToReplyListener;
		this.guanzhuOnclickListener = guanzhuOnclickListener;
		this.dianzanListener = dianzanListener;
	}

	public void clearList() {
		this.commentList.clear();
	}

	public void updateList(ArrayList<TieZiBaoHanReply.GetAList> commentList) {
		this.commentList.addAll(commentList);
	}

	@Override
	public int getCount() {
		return commentList.size();
	}

	@Override
	public TieZiBaoHanReply.GetAList getItem(int position) {
		return commentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_list, null);
			viewHolder = new ViewHolder();
			// viewHolder.iv_user_photo = (ImageView) convertView
			// .findViewById(R.id.iv_user_photo);
			viewHolder.tv_user_name = (TextView) convertView
					.findViewById(R.id.tv_title);
			viewHolder.tv_user_comment = (TextView) convertView
					.findViewById(R.id.tv_content);
			viewHolder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			viewHolder.btn_comment_reply = (TextView) convertView
					.findViewById(R.id.tetPing);
			viewHolder.txt_time = (TextView) convertView
					.findViewById(R.id.textTime);
			viewHolder.lv_user_comment_replys = (MyListView) convertView
					.findViewById(R.id.lv_user_comment_replys);
			viewHolder.tetzan = (TextView) convertView
					.findViewById(R.id.tetzan);
			viewHolder.imgZan = (ImageView) convertView
					.findViewById(R.id.imgZan);
			viewHolder.tetPing = (TextView) convertView
					.findViewById(R.id.tetPing);
			viewHolder.gridview = (NoScrollGridView) convertView
					.findViewById(R.id.gridview);
			viewHolder.btn_comment_reply.setTag(position);
			viewHolder.textGuanzhu = (TextView) convertView
					.findViewById(R.id.textGuanzhu);
			viewHolder.imgPing = (ImageView) convertView
					.findViewById(R.id.imgPing);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		TieZiBaoHanReply.GetAList comment = getItem(position);
		viewHolder.tv_user_name.setText(comment.getauthor());
		viewHolder.tv_user_comment.setText("\t"
				+ commentList.get(position).getMessage());
		// 设置评论列表的点击效果透明
		// viewHolder.lv_user_comment_replys.setSelector(new ColorDrawable(
		// Color.TRANSPARENT));
		viewHolder.txt_time
				.setText(new SimpleDateFormat("yyyy-MM-dd").format(Long
						.parseLong(commentList.get(position).getDateline()) * 1000));
		viewHolder.tetPing.setOnClickListener(replyToReplyListener);
//		if (Integer.parseInt(commentList.get(position).getIsLike()) == 1) {
//			viewHolder.imgZan.setImageResource(R.drawable.like_select);
//		} else {
			viewHolder.imgZan.setImageResource(R.drawable.like_unselect);
//		}
		// 判断当前评论是否有回复
		System.out.println(commentList.get(position).getReplyInfo().getList()
				+ "这是评论及回复");
		if (commentList.get(position).getReplyInfo().getList() != null) {
			// if(commentList.get(position).getReplyInfo().getList().size()<=0){
			// viewHolder.lv_user_comment_replys.setVisibility(View.GONE);
			// }else{
			viewHolder.lv_user_comment_replys
					.setAdapter(new MyTieZiCommentReplyAdapter(context, commentList
							.get(position).getReplyInfo().getList(), position,
							replyToReplyListener));
			// }
		}
		// else {
		// viewHolder.lv_user_comment_replys.setVisibility(View.GONE);
		// }

//		if (commentList.get(position).getIsFocus().equals("0")) {
			viewHolder.textGuanzhu.setText("删除");
//			viewHolder.textGuanzhu.setTextColor(context.getResources()
//					.getColor(R.color.main_color));
//		} else {
//			viewHolder.textGuanzhu.setText("已关注");
//			viewHolder.textGuanzhu.setTextColor(Color.BLACK);
//		}
		viewHolder.textGuanzhu.setTag(commentList.get(position).getAuthorId());
		viewHolder.textGuanzhu.setTag(R.id.tag_first, commentList.get(position)
				.getIsFocus());
		viewHolder.textGuanzhu.setOnClickListener(guanzhuOnclickListener);
		viewHolder.imgZan.setTag(position);
		viewHolder.tetzan.setTag(position);
		viewHolder.imgZan.setOnClickListener(dianzanListener);
		viewHolder.tetzan.setOnClickListener(dianzanListener);
		// 记录点击回复按钮时对应的position,用于确定所回复的对象
		viewHolder.imgPing.setTag(position);
		viewHolder.btn_comment_reply.setTag(position);
		viewHolder.imgPing.setOnClickListener(replyToCommentListener);
		viewHolder.btn_comment_reply.setOnClickListener(replyToCommentListener);
		final ArrayList<String> arrayImage = commentList.get(position).getImg();
		// if (arrayImage == null || arrayImage.size() == 0) { //
		// 没有图片资源就隐藏GridView
		// viewHolder.gridview.setVisibility(View.GONE);
		// } else {
		viewHolder.gridview.setAdapter(new NoScrollGridAdapter(context,
				arrayImage));
		// }
		// 使用ImageLoader加载网络图片
		XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
				+ commentList.get(position).getHead(), viewHolder.iv_avatar);
		// 点击回帖九宫格，查看大图
		viewHolder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, arrayImage);
			}
		});
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(context, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		context.startActivity(intent);
	}

	public class ViewHolder {
		private ImageView iv_avatar, imgZan, imgPing;
		private TextView txt_time;
		private ImageView iv_user_photo; // 评论者 头像
		private TextView tv_user_name; // 评论者 昵称
		private TextView tv_user_comment; // 评论者 一级品论内容
		// private TextView tv_user_comment_date; //
		private TextView btn_comment_reply; // 评论者 二级评论按钮
		private MyListView lv_user_comment_replys; // 评论者 二级品论内容列表
		private NoScrollGridView gridview;
		TextView tetzan, textGuanzhu, tetPing;
	}
}
